package com.demo.client;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneStateChangeListener extends PhoneStateListener{

	private static final String TAG = LogUtil.getClassName(PhoneStateChangeListener.class);
	
	private NotificationService notificationService = null;
	
	public PhoneStateChangeListener(NotificationService notificationService) {
		
		this.notificationService = notificationService;
	
	}

	@Override
	public void onDataConnectionStateChanged(int state) {
		super.onDataConnectionStateChanged(state);
		
		Log.d(TAG, "Phone State:"+getConnectState(state));
		
		if(state == TelephonyManager.DATA_DISCONNECTED)
		{
			notificationService.reconnect();
		}
	}

	/**
	 * 获取当前连接状态
	 * 
	 * @param state
	 * @return
	 */
	private String getConnectState(int state)
	{
		switch(state)
		{
		case TelephonyManager.DATA_DISCONNECTED:
			return "DATA_DISCONNECTED";
		case TelephonyManager.DATA_CONNECTING:
			return "DATA_CONNECTING";
		case TelephonyManager.DATA_CONNECTED:
			return "DATA_CONNECTED";
		}
		return "DATA<UNKONW>";
	}
}
